package com.bjfu.gmms.servlet;




import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.CatalogMapper;
import com.bjfu.gmms.model.Catalog;
import com.bjfu.gmms.service.CatalogService;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/catalogController")
public class CatalogController extends HttpServlet {

    private CatalogService catalogService;

    @Override
    public void init() throws ServletException {
        // 手动实例化 Service 和 Mapper
        CatalogMapper catalogMapper = MyBatisUtil.getSqlSessionFactory("development").openSession().getMapper(CatalogMapper.class);
        catalogService = new CatalogService(catalogMapper);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取查询数据
        List<Catalog> catalogList = catalogService.getCatalogList();
        request.setAttribute("catalogList", catalogList);

        // 跳转到 JSP 页面
        RequestDispatcher dispatcher = request.getRequestDispatcher("/viewCatalog.jsp");
        dispatcher.forward(request, response);
    }
}

